//
//  StatusCell.swift
//  HMWeibo08
//
//  Created by 唐万超 on 15/8/3.
//  Copyright © 2015年 唐万超. All rights reserved.
//

import UIKit

/// 控件间距
let statusCellControlMargin: CGFloat = 12.0

/// 枚举定义不同cell的标识符
enum StatusCellIdentifier: String {

    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    /// 静态函数，根据转发微博确定cell的id类型
    static func cellId(status: Status) -> String {
    
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}

class StatusCell: UITableViewCell {
    
    /// 微博数据模型
    var statuses: Status? {
        didSet {
        
            topView.statuses = statuses
            contentLabel.text = statuses?.text
            
            // 传递微博数据
            pictureView.statuses = statuses
            
            // 此处拿到微博数据之后再进行设置配图的尺寸
            // 设置配图视图的尺寸
            pictureWidthCons?.constant = pictureView.bounds.width
            pictureHeightCons?.constant = pictureView.bounds.height
            
            // 配图视图高度为零则顶部约束设置为0
            pictureTopCons?.constant = pictureView.bounds.height == 0 ? 0 : statusCellControlMargin
        }
    }
    
    /// 图片宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    
    /// 图片高度约束
    var pictureHeightCons: NSLayoutConstraint?
    
    /// 图片顶部视图的约束
    var pictureTopCons: NSLayoutConstraint?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 计算行高
    func rowHeight(status: Status) -> CGFloat{
    
        self.statuses = status
        
        // 强行更新布局，所有空间的frame都发生变化
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    func setUpUI() {
        
        // 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 设置约束
        /// 顶部视图约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 57))
        
        /// 文本约束
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        
        // 文本宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -(2 * statusCellControlMargin) ))
        
        /// 配图约束
//       let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: statusCellControlMargin))
//        pictureWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        
//        pictureTopCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        
        /// 底部视图的约束
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
    }
    
    // MARK: - 懒加载控件
    
    /// 顶部视图
    private lazy var topView: StatusesTopView =  StatusesTopView()
    
    /// 内容文本
    lazy var contentLabel: UILabel = {
    
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        
        label.numberOfLines = 0
        
        // 设置文本折行
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellControlMargin
        
        return label
        
    }()
    
    /// 配图视图
    lazy var pictureView: StatusPictureView = StatusPictureView()
    
    /// 底部视图
    lazy var bottomView: StatusBottomView = StatusBottomView()

}
